﻿Partial Public Class WorkOrderExplorer

    Public Overridable Sub AfterViewRegistered(ByVal sender As Object, ByVal e As ViewOperationEventArgs)
        Dim view As GridView = DirectCast(e.View, GridView)

        Select Case view.Tag.ToString
            Case "MRPResultsWithRev_Express"
                view.ExpandAllGroups()
            Case "MRPShopListWithRev_Express"
                view.ExpandAllGroups()
        End Select
    End Sub

    ''' <summary>
    ''' Validation for Rows of what ever levels
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub InitNewRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim aView As GridView = CType(sender, GridView)
        Dim aRow As DataRow = aView.GetFocusedDataRow()
        Dim aIPLProc As IPLProcess = Nothing

        Select Case aRow.Table.TableName
            Case "MRPHeader_Express" 'Le Master Row
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPHeader_PL)

            Case "MRPMasterPlan_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPMasterPlan_PL)

            Case "MRPProdListWithRev_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPProdList_PL)
                aRow("MRPProdListRevID") = Guid.NewGuid()
                aRow("MRPRevID_FK") = MRPRevID
                aRow("MRPProdListID") = Guid.NewGuid()
                aRow("MRPHeaderID_FK") = MRPID


            Case "MRPResults_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPResults_PL)

            Case "MRPRevision_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPRevision_PL)
                aRow("IsActivated") = 0
                aRow("MRPRevID") = Guid.NewGuid()
                aRow("MRPID_FK") = MRPID

            Case "MRPShopList_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPShopList_PL)

            Case "MRPTransactionLog_Express"
                aIPLProc = PLBase.CreateIPLProcessOf(PLTypeOf.MRPTransactionLog_PL)
            Case Else
        End Select

        If aIPLProc IsNot Nothing Then
            aIPLProc.NewRecord(aRow)
        End If

    End Sub

    'Public Sub Init_TreeActionOptionList(sender As Object, e As EventArgs)
    '    Me.TreeActionOptionList.Add(New ActionOption("MRP", "ACT_00081", "Create New MRP Header", AddressOf TreeListMenu_NewMRPHeader, True, True, 0))
    '    Me.TreeActionOptionList.Add(New ActionOption("MRPHeader", "ACT_00083", "Create New Revision", AddressOf TreeListMenu_NewMRPRevision, True, True, 1))
    '    Me.TreeActionOptionList.Add(New ActionOption("MRPHeaderRev", "ACT_00084", "Copy to New Revision", AddressOf TreeListMenu_CopyToNewRevision, True, True, 2))
    '    Me.TreeActionOptionList.Add(New ActionOption("MRPHeaderRev", "ACT_00085", "Generate Work Order", AddressOf TreeListMenu_GenerateMRPTable, True, True, 2))
    'End Sub

    Public Sub TreeActionOptionListLoad()
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("MRP", "ACT_00081", "Create New MRP Header", AddressOf TreeListMenu_NewMRPHeader, True, True, 0))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("MRPHeader", "ACT_00083", "Create New Revision", AddressOf TreeListMenu_NewMRPRevision, True, True, 1))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("MRPHeaderRev", "ACT_00084", "Copy to New Revision", AddressOf TreeListMenu_CopyToNewRevision, True, True, 2))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("MRPHeaderRev", "ACT_00085", "Generate Work Order", AddressOf TreeListMenu_GenerateMRPTable, True, True, 2))
    End Sub

    Public Overridable Sub ValidateRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs)
        Dim aRow As DataRow = CType(e.Row, DataRowView).Row
        Select Case aRow.Table.TableName
            Case "MRPHeader_Express" 'Le Master Row
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPHeader_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPMasterPlan_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPMasterPlan_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPProdListWithRev_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPProdList_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPResults_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPResults_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPRevision_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPRevision_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPShopList_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPShopList_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case "MRPTransactionLog_Express"
                Using pl As PLBase = SystemBoxSDK.PL.PLBase.CreateInstanceOf(PLTypeOf.MRPTransactionLog_PL)
                    pl.ValidateRow(aRow)
                End Using
            Case Else
        End Select
    End Sub

    Public Overridable Sub ProdList_ContextMenuBeforePopup(sender As Object, e As EventArgs)
        Dim aMenu As GridViewMenu = CType(sender, GridViewMenu)
        Dim row As DataRow = aMenu.View.GetFocusedDataRow()


        'if aMenu.View.Name = "MRPProdListWithRev_Express" Then

        If row IsNot Nothing Then
            Dim i As Integer = 0
            While i < aMenu.Items.Count
                'If (aMenu.Items(i).Caption.Contains("Revise") OrElse aMenu.Items(i).Caption.Contains("Limit")) Then

                '    If imExplorer.IMActivated OrElse Not imExplorer.SpecEnabled OrElse Not CBool(row("IsCopy")) Then
                '        aMenu.Items(i).Enabled = False
                '    End If
                'End If

                If (aMenu.Items(i).Caption.Contains("Revise") OrElse aMenu.Items(i).Caption.Contains("Limit")) OrElse aMenu.Items(i).Caption.Contains("Delete") Then
                    If row("IsActivated") Then
                        aMenu.Items(i).Enabled = False

                    ElseIf CBool(row("IsCopy")) And aMenu.Items(i).Caption.Contains("Delete") Then
                        aMenu.Items(i).Enabled = False

                    ElseIf (Not CBool(row("IsCopy")) And (aMenu.Items(i).Caption.Contains("Revise") OrElse aMenu.Items(i).Caption.Contains("Limit"))) Then
                        aMenu.Items(i).Enabled = False
                    End If
                End If


                i += 1
            End While
        End If
    End Sub

    Public Overridable Sub TreeListMenu_NewMRPHeader(ByVal sender As Object, ByVal e As EventArgs)
        ShowMRPHeader(Guid.Empty)
        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = "New MRP Header"
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
    End Sub

    Public Overridable Sub TreeListMenu_NewMRPRevision(ByVal sender As Object, ByVal e As EventArgs)
        ShowMRPRevision(Guid.Empty)
        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = "New MRP Header Revision"
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
    End Sub

    Public Overridable Sub TreeListMenu_CopyToNewRevision(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor

        Dim aPL As PLBase = PLBase.CreateInstanceOf(PLTypeOf.MRPRevision_PL, "MRPRevision_Express")
        Dim aMRPRevPL As MESSDK.MRPRevision_PL = TryCast(aPL, MESSDK.MRPRevision_PL)

        'Dim node As TreeListNode = CType(CType(sender, DXMenuItem).Tag, TreeListNode)
        Dim MRPRevID As Guid = NodeDataRow(Me.KeyFieldName)

        aMRPRevPL.CopyToNewRevision(MRPRevID)

        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub TreeListMenu_GenerateMRPTable(ByVal sender As Object, ByVal e As EventArgs)
        'Dim node As TreeListNode = CType(CType(sender, DXMenuItem).Tag, TreeListNode)
        Dim MRPRevID As Guid = NodeDataRow(Me.KeyFieldName)
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

        Try
            Me.Cursor = Cursors.WaitCursor
            sqlcmd = MetaQuery.GetSPCmdByMetaName("MRPRevision_Express_GenerateMRPTable")

            sqlcmd.Parameters("@MRPRevID").Value = MRPRevID
            sqlcmd.Parameters("@SaveResults").Value = 1

            sqlcmd.ExecuteNonQuery()
            MessageWindow.ShowMessage("WO Generated", MsgBoxStyle.Information)

        Catch sqlex As System.Data.SqlClient.SqlException
            MessageWindow.ShowMessage(sqlex)
        Finally
            If sqlcmd IsNot Nothing Then
                sqlcmd.Connection.Close()
                sqlcmd.Dispose()
            End If
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Overridable Sub ProdListMenu_Revise(ByVal sender As System.Object, ByVal e As EventArgs)
        MessageWindow.ShowMessage("In Dev.")

        'Me.Cursor = Cursors.WaitCursor
        'Dim view As GridView = CType(sender, GridView)

        'If view IsNot Nothing Then
        '    Dim aRow As DataRow = view.GetFocusedDataRow()

        '    If aRow IsNot Nothing AndAlso CBool((aRow("IsCopy"))) Then
        '        Dim MRPProdListID As Guid = CType(aRow("MRPProdListID"), Guid)

        '        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        '        sqlcmd.Parameters("@MRPProdListID").Value = MRPProdListID

        '        Try
        '            sqlcmd = MetaQuery.GetSPCmdByMetaName("MRPProdListWithRev_Express_Revise")


        '            sqlcmd.ExecuteNonQuery()
        '            'Refresh Screen
        '            aGridControl.ReloadGrid()

        '        Catch sqlex As System.Data.SqlClient.SqlException
        '            If sqlex.Number = 50000 Then
        '                MessageWindow.ShowMessage("Can't Revise this Product since it is use for an Item Master Test.")
        '            Else
        '                MessageWindow.ShowMessage(sqlex)
        '            End If
        '        Finally
        '            If sqlcmd IsNot Nothing Then
        '                sqlcmd.Connection.Close()
        '                sqlcmd.Dispose()
        '            End If
        '        End Try
        '    Else
        '        MessageWindow.ShowMessage("No need to Revise, this Product is not reference more than once.")
        '    End If
        'End If
        'Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ProdListMenu_Limit(ByVal sender As System.Object, ByVal e As EventArgs)
        MessageWindow.ShowMessage("In Dev.")

        'Me.Cursor = Cursors.WaitCursor
        'Dim view As GridView = CType(sender, GridView)

        'If view IsNot Nothing Then
        '    Dim aRow As DataRow = view.GetFocusedDataRow()

        '    If aRow IsNot Nothing AndAlso CBool((aRow("IsCopy"))) Then
        '        Dim MRPProdListID As Guid = CType(aRow("MRPProdListID"), Guid)

        '            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        '            sqlcmd.Parameters("@MRPProdListID").Value = MRPProdListID

        '            Try
        '            sqlcmd = MetaQuery.GetSPCmdByMetaName("MRPProdListWithRev_Express_Limit")


        '                sqlcmd.ExecuteNonQuery()
        '                'Refresh Screen
        '                aGridControl.ReloadGrid()

        '            Catch sqlex As System.Data.SqlClient.SqlException
        '                If sqlex.Number = 50000 Then
        '                MessageWindow.ShowMessage("Can't Limit this Product since it is use for an Item Master Test.")
        '                Else
        '                    MessageWindow.ShowMessage(sqlex)
        '                End If
        '            Finally
        '                If sqlcmd IsNot Nothing Then
        '                    sqlcmd.Connection.Close()
        '                    sqlcmd.Dispose()
        '                End If
        '            End Try
        '        Else
        '        MessageWindow.ShowMessage("No need to Limit, this Product is not reference more than once.")
        '        End If
        'End If
        'Me.Cursor = Cursors.Default
    End Sub

End Class
